﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DBProvider;
using DBProvider.Providers;

namespace HealthCommunity.Controls
{
	public partial class UCAddUser : System.Web.UI.UserControl
	{
		public string ServerURL
		{
			get
			{
				if(Request.Url.Port > 0)
				{
					return string.Format("http://{0}:{1}", Request.Url.Host, Request.Url.Port.ToString());
				}
				else
				{
					return "http://" + Request.Url.Host;
				}
			}
		}

		protected void Page_Load(object sender, EventArgs e)
		{
			if(!IsPostBack)
			{
				BindDrugStore();
				BindRole();
			}
		}

		protected void btnSave_Click(object sender, EventArgs e)
		{
			var u = new User();

			u.AddDate = DateTime.Now;
			u.DrugStoreID = int.Parse(ddlDrugStore.SelectedValue);
			u.RoleID = int.Parse(ddlRole.SelectedValue);
			u.Email = txtEmail.Text.Trim();
			u.Position = txtPosition.Text.Trim();
			u.QQ = txtQQ.Text.Trim();
			u.UserName = txtQQ.Text.Trim();

			var userProvider = new UserProvider();

			try
			{
				var msg = string.Empty;
				u = userProvider.CreateUser(u, out msg);

				if(u == null)
				{
					ltrMsg.Text = msg;
				}
				else
				{
					var to = u.Email;
					var subject = "完整用户信息";
					var url = string.Format("{0}/UserEdit.aspx?userid={1}", ServerURL, u.UserID.ToString());
					var body = "进入下面链接完整用户信息，否则无法登入。" + url;

					CommonHelper.SendMail(to, subject, body);

					ltrMsg.Text = "用户添加成功，提醒邮件已发送至用户邮箱";
				}
			}
			catch(System.Net.Mail.SmtpException smtpexc)
			{
				ltrMsg.Text = smtpexc.Message;
			}
			catch(Exception exc)
			{
				ltrMsg.Text = exc.Message;
			}
		}

		private void BindDrugStore()
		{
			var drugStoreProvider = new DrugStoreProvider();

			ddlDrugStore.Items.Clear();

			var lstAllDrugStore = drugStoreProvider.GetAllDrugStore();

			var u = SiteContext.Current.CurrentUser;

			if(u != null && u.Role.RoleName.ToLower().Equals("admin"))
			{
				ddlDrugStore.DataSource = lstAllDrugStore;
				ddlDrugStore.DataBind();
			}
			else if(u != null)
			{
				var data = new List<DrugStore>();
				data.Add(u.DrugStore);
				ddlDrugStore.DataSource = data;
				ddlDrugStore.DataBind();
			}
		}

		private void BindRole()
		{
			var userProvider = new UserProvider();

			ddlRole.Items.Clear();

			var lstRole = userProvider.GetAllRole();

			var u = SiteContext.Current.CurrentUser;

			//if(u != null && u.Role.RoleName.ToLower().Equals("admin"))
			//{
			//  ddlRole.DataSource = lstAllRole;
			//  ddlRole.DataBind();
			//}
			//else 
			if(u != null && u.Role.RoleName.ToLower().Equals("admin"))
			{
				foreach(var r in lstRole)
				{
					if(r.RoleName.ToLower().Equals("admin"))
					{
						lstRole.Remove(r);
						break;
					}
				}
			}

			if(u != null && u.Role.RoleName.ToLower().Equals("storemanager"))
			{
				foreach(var r in lstRole)
				{
					if(r.RoleName.ToLower().Equals("admin"))
					{
						lstRole.Remove(r);
						break;
					}
				}

				foreach(var r in lstRole)
				{
					if(r.RoleName.ToLower().Equals("storemanager"))
					{
						lstRole.Remove(r);
						break;
					}
				}
			}
			//else 
			if(u != null && u.Role.RoleName.ToLower().Equals("storeadmin"))
			{
				foreach(var r in lstRole)
				{
					if(r.RoleName.ToLower().Equals("admin"))
					{
						lstRole.Remove(r);
						break;
					}
				}
				
				foreach(var r in lstRole)
				{
					if(r.RoleName.ToLower().Equals("storemanager"))
					{
						lstRole.Remove(r);
						break;
					}
				}
				
				foreach(var r in lstRole)
				{
					if(r.RoleName.ToLower().Equals("storeadmin"))
					{
						lstRole.Remove(r);
						break;
					}
				}
			}

			ddlRole.DataSource = lstRole;
			ddlRole.DataBind();
		}
	}
}
